<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        body{
            height: 100vh;
            background: radial-gradient(#559, #afe);
        }
        .container{
            width: 640px;
            height: 360px;
            position:absolute;
            left: 50%;
            top: 50%;
            transform: translateX(-50%) translateY(-50%);
        }
        .arrow{
            position:absolute;
            width: 60px;
            height: 60px;
            background: white;
            border-radius: 50%;
            top: 150px;
            text-align:center;
            line-height: 60px;
            font-size: 20px;
            user-select: none;
            cursor: pointer;
        }

        .right {
            right: 0;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Slider extends React.Component{
            state = {
                //图片的编号
                num: 1,
                index: 0, 
                images: [
                    './images/1.jpg',
                    './images/100.jpg',
                    './images/259.jpg',
                    './images/639.jpg',
                ]
            }

            //1 + N 
            render(){
                return <div className="container">
                        <img width="100%" src={"./images/"+this.state.num+".jpg"} alt="" />
                        <div class="left arrow" onClick={this.prev}> &lt; </div>
                        <div class="right arrow" onClick={this.next}> &gt; </div>
                    </div>
            }

            //声明一个方法   let n = 100;   console.log(n++)
            next = () => {
                //获取当前的 num 值
                let {num} = this.state;
                //判断
                if(num >= 5){
                    num = 1;
                }else{
                    num += 1;
                }

                this.setState({
                    num
                })
            }

            //声明方法 上一张
            prev = () => {
                //获取 num 的值
                let {num} = this.state;
                //判断
                if(num <= 1){
                    num = 5;
                }else{
                    num -= 1;
                }
                //设置
                this.setState({
                    num
                })
            }
        }
        //渲染
        ReactDOM.render(<Slider/>, document.querySelector("#root"));
    </script>
</body>
</html>